package bookez.view.bean.backing;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIMessage;
import javax.faces.context.FacesContext;

import bookez.model.businessobject.BusType;
import bookez.model.businessobject.Route;
import bookez.model.businessobject.TripDailyRecord;
import bookez.model.businessobject.TripSchedule;
import bookez.model.businessobject.TripStatus;
import bookez.model.exception.BusinessLogicLayerException;
import bookez.model.service.TripScheduleService;
import bookez.view.bean.RouteBean;
import bookez.view.bean.TripScheduleBean;
import bookez.view.util.FacesUtil;
import bookez.view.util.ProcessMessage;


@ManagedBean
@SessionScoped
public class NewTripScheduleForm extends BaseBackingBean {

	public void createNewSchedule() {
		
		process();
		
	}//end method createNewSchedule


	/*----------------------------------------
	 * Methods
	 *----------------------------------------*/
	
	private void process() {
		
		// Update Route
		getModel().setRoute( getSelectedRoute() );
		
		// Update BusType
		getModel().setBusType( getSelectedBusType() );
		
		// Set Status
		getModel().setStatus( TripStatus.ACTIVE );
		
		TripSchedule schedule = TripScheduleBean.createTripSchedule( getModel() );
		
		// Generate TripDailyRecord
		try {
			List<TripDailyRecord> records = getServiceLocator().getTripDailyRecordService().generateRecord( schedule );
			schedule.setDailyRecords( records );
		}//end try
		
		catch (BusinessLogicLayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//end catch
		
		
		try {
			getServiceLocator().getTripScheduleService().createTripSchedule( schedule );
			getModel().clear();
			ProcessMessage.processInfo( getMessage().getClientId( FacesUtil.getFacesContext() ),
										TripScheduleBean.MessageId.SCHEDULE_CREATED,
										new Object[] { schedule.getTripCode() } );
			
		}//end try
		
		catch ( BusinessLogicLayerException busex ) {
			busex.printStackTrace();
		}//end catch
		
	}//end method process
	
	private Route processRoute(){		
		Route route = RouteBean.createRoute( getRouteBean() );
		try {
			getServiceLocator().getRouteService().saveRoute( route );
			return route;
		} catch (BusinessLogicLayerException e) {			
			e.printStackTrace();
			return null;
		}
	}//end method processRoute
	
	
	/*----------------------------------------
	 * Validators
	 *----------------------------------------*/
	
	public void validateTripCode( FacesContext context, UIComponent ui, Object value ) {
		
		String tripCode = (String)value;
		
		TripScheduleService service = getServiceLocator().getTripScheduleService();
		
		try {
			TripSchedule token = service.getByTripCode( tripCode );
			if( token != null ) {	
				ProcessMessage.processError( ui.getClientId( context ), TripScheduleBean.MessageId.SCHEDULE_EXISTS, new Object[] { tripCode } );
			}
		} catch (BusinessLogicLayerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//end catch
		
	}//end method validateTripCode
	
	/*----------------------------------------
	 * Properties
	 *----------------------------------------*/
	
	public RouteForm getRouteForm() { return this.routeForm; }
	public void setRouteForm( RouteForm value ) { this.routeForm = value; }
	
	public BusTypeViewBox getBusTypeBox() { return this.busTypeBox; }
	public void			  setBusTypeBox( BusTypeViewBox value ) { this.busTypeBox = value; }
	
	public TripScheduleDateTimeForm getScheduleForm() { return this.scheduleForm; }
	public void						setScheduleForm( TripScheduleDateTimeForm value ) { this.scheduleForm = value; }
	
	public Route getSelectedRoute() { return processRoute(); }
	//public void setSelectedRoute(Route selectedRoute) { this.selectedRoute = selectedRoute; }
	
	public BusType getSelectedBusType() { return selectedBusType; }
	public void setSelectedBusType(BusType selectedBusType) { this.selectedBusType = selectedBusType; }
	
	public RouteBean getRouteBean() { return routeBean; }
	public void setRouteBean(RouteBean routeBean) { this.routeBean = routeBean; }
	
	public TripScheduleBean getModel()	{ return this.model; }
	public void				setModel( TripScheduleBean value ) { this.model = value; }
	
	public void setMessage(UIMessage message) {
		this.message = message;
	}


	public UIMessage getMessage() {
		return message;
	}
	
	/*----------------------------------------
	 * Instance Varibles
	 *----------------------------------------*/



	@ManagedProperty ( name="routeForm", value = "#{routeForm}" )
	private RouteForm routeForm;
	
	@ManagedProperty ( name="busTypeBox", value="#{busTypeViewBox}")
	private BusTypeViewBox busTypeBox;
	
	@ManagedProperty ( name="scheduleForm", value="#{tripScheduleDateTimeForm}")
	private TripScheduleDateTimeForm scheduleForm;
	
	//private Route selectedRoute;
	private BusType selectedBusType;
	
	@ManagedProperty( value="#{routeBean}" )
	private RouteBean routeBean;
	
	@ManagedProperty( name="model", value="#{tripScheduleBean}")
	private TripScheduleBean model;
	
	private UIMessage message;
	
	private static final long serialVersionUID = -3047053658445948680L;	
}//end class NewTripScheduleForm
